package com.voip.sipphone;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.InetAddress;

/**
 * Created by RXJ on 2018/1/2.
 */
public class TextClient extends JFrame implements MessageProcessor {
    private SipLayer sipLayer;
    private JTextField fromAddress;
    private JLabel fromLbl;
    private JLabel receivedLbl;
    private JTextArea receivedMessages;
    private JScrollPane receivedScrollPane;
    private JButton sendBtn;
    private JButton RegsiterBtn;
    private JLabel sendLbl;
    private JTextField sendMessages;
    private JTextField toAddress;
    private JLabel toLbl;



    public static void main(String[] args) {
        String username = "816";
        int pid =0;
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        String name = runtime.getName();
        System.out.println("当前进程的标识为："+name);
        int index = name.indexOf("@");
        if (index != -1) {
             pid = Integer.parseInt(name.substring(0, index));
            System.out.println("当前进程的PID为："+pid);
        }
        try {

            int port = pid;
            String ip = InetAddress.getLocalHost().getHostAddress();
            System.out.println("--username = " + username + "\n--port = " + port + "\n--ip = " + ip);
            SipLayer sipLayer = new SipLayer(username, ip, port);
            TextClient tc = new TextClient(sipLayer);
            sipLayer.setMessageProcessor(tc);
            // tc.show();
            tc.setVisible(true);
        } catch (Throwable e) {
            System.out.println("Problem initializing the SIP stack.");
            e.printStackTrace();
            System.exit(-1);
        }
    }


    private static void printUsage() {
        System.out.println("Syntax:");
        System.out.println("  java -jar textclient.jar <username> <port>");
        System.out.println("where <username> is the nickname of this user");
        System.out.println("and <port> is the port number to use. Usually 5060 if not used by another process.");
        System.out.println("Example:");
        System.out.println("  java -jar textclient.jar snoopy71 5061");
    }

    public TextClient(SipLayer sip) {
        super();
        sipLayer = sip;
        initWindow();
        String from = "sip:" + sip.getUsername() + "@" + sip.getHost() + ":" + sip.getPort();
        String to = "sip:" + sip.getUsername() + "@" + SipPhone.ipAddr;
        this.fromAddress.setText(from);
        this.toAddress.setText(to);
    }

    private void initWindow() {
        receivedLbl = new JLabel();
        sendLbl = new JLabel();
        sendMessages = new JTextField();
        receivedScrollPane = new JScrollPane();
        receivedMessages = new JTextArea();
        fromLbl = new JLabel();
        fromAddress = new JTextField();
        toLbl = new JLabel();
        toAddress = new JTextField();
        sendBtn = new JButton();
        RegsiterBtn = new JButton();

        getContentPane().setLayout(null);
        setTitle("SipPhoneLayer");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
        receivedLbl.setText("Received Messages:");
        receivedLbl.setAlignmentY(0.0F);
        receivedLbl.setPreferredSize(new java.awt.Dimension(25, 100));
        getContentPane().add(receivedLbl);
        receivedLbl.setBounds(5, 0, 136, 20);
        sendLbl.setText("Send Message:");
        getContentPane().add(sendLbl);
        sendLbl.setBounds(5, 150, 90, 20);
        getContentPane().add(sendMessages);
        sendMessages.setBounds(5, 170, 270, 20);
        receivedMessages.setAlignmentX(0.0F);
        receivedMessages.setEditable(false);
        //设置为不可编辑
        receivedMessages.setLineWrap(true);
        //设置可换行
        receivedMessages.setWrapStyleWord(true);
        //设置换行方式为单词边界换行
        receivedScrollPane.setViewportView(receivedMessages);
        receivedScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        getContentPane().add(receivedScrollPane);
        receivedScrollPane.setBounds(5, 20, 270, 130);
        fromLbl.setText("From:");
        getContentPane().add(fromLbl);
        fromLbl.setBounds(5, 200, 35, 15);
        getContentPane().add(fromAddress);
        fromAddress.setBounds(40, 200, 235, 20);
        fromAddress.setEditable(false);
        toLbl.setText("To:");
        getContentPane().add(toLbl);
        toLbl.setBounds(5, 225, 35, 15);
        getContentPane().add(toAddress);
        toAddress.setBounds(40, 225, 235, 21);
        //发送按钮
        sendBtn.setText("Send");
        sendBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                sendBtnActionPerformed(evt);
            }
        });
        getContentPane().add(sendBtn);
        sendBtn.setBounds(150, 255, 100, 25);
        //注册按钮
        RegsiterBtn.setText("Register");
        RegsiterBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                RegsiterBtnActionPerformed(evt);
            }
        });
        getContentPane().add(RegsiterBtn);
        RegsiterBtn.setBounds(50, 255, 100, 25);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - 288) / 2, (screenSize.height - 310) / 2, 288, 320);
    }

    /**
     * 信息发送请求
     * @param evt
     */
    private void sendBtnActionPerformed(ActionEvent evt) {
        try {

            sipLayer.processInvite("13288727108");
        } catch (Throwable e) {
            e.printStackTrace();
            this.receivedMessages.append("ERROR sending message: " + e.getMessage() + "\n");
        }
    }

    /**
     * 发起注册请求
     * @param evt
     */
    private void RegsiterBtnActionPerformed(ActionEvent evt){
        try {
            Register.sendRegister();
        }catch (Exception ex){
            ex.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
            this.receivedMessages.append("ERROR sending message: " + e.getMessage() + "\n");
        }
    }
    public void processMessage(String sender, String message) {
        this.receivedMessages.append("From " + sender + ": " + message + "\n");
    }

    public void processError(String errorMessage) {
        this.receivedMessages.append("ERROR: " + errorMessage + "\n");
    }

    public void processInfo(String infoMessage) {
        this.receivedMessages.append(infoMessage + "\n");
    }
}
